import requests
import time
import sys
sys.path.append("../")
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
from frameworks.cores.LoadClass import *
from services.CodesService import *
from services.DayKlineService import *
from frameworks.utils.RedisUtil import *
import json
import time

loader = LoadClass()
redis = RedisUtil()

"""
#f2:最新价 f3:涨幅 f4:涨跌额 f5:成交量 f6成交额 f7:振幅
#f8:换手率 f9:市盈率 f10:量比 f11: f12:代码 f14:股票名称 f15:最高
#f16:最低 f17:今开 f18:昨收 f20:总市值 f21:流通市值
#f22:
"""

base = StockBaseInfo()
klineService = DayKlineService()
def getMiniteData(code):
    print(code)
    data = klineService.getThirtyData(code)
    score = 0
    n = 1
    zf = 0
    for row in data:
        close = float(row["close"])
        pre = float(row["pre_close"])
        money = float(row["money"])
        flow_money = close * float(row["flow_volume"])
        if close > pre:
            zf = round((money*100)/flow_money,2)
        else:
            zf = -1*round((money*100)/flow_money,2)
        score += zf*n
        n += 1
    return [round(score,2),round(zf,2)]

codes = []
codeService = CodeService()
#prettyTable = PrettyTableUtil(["代码", "名称", "score", "涨幅","流通市值","分时图","日线图"])
newParam = {}
obj = klineService.getTenHasZhangting()
m = 0
ck = "day_score_money_cache"
allrs = []
for line in obj:
    #if m > 100:
    #    break
    score,zf = getMiniteData(line["code"])
    """
    prettyTable.addRow([line["code"], line["codename"], score, zf,round(float(line["flow_money"])/100000000,2),
                        "<img width=545 height=300 src='http://image2.sinajs.cn/newchart/min/n/" + line["code"].lower() + ".gif?'>",
                        "<img width=545 height=300 src='http://image2.sinajs.cn/newchart/daily/n/" + line["code"].lower() + ".gif?'>",
                        ])                    
    """
    allrs.append({
        "code": line["code"],
        "codename": line["codename"],
        "score": score,
        "zf": zf,
        "money": str(round(line["flow_money"] / 100000000)) + " 亿元",
        "divtime": "<img src='http://image2.sinajs.cn/newchart/min/n/" + line["code"].lower() + ".gif?'>",
        "daykline": "<img src='http://image2.sinajs.cn/newchart/daily/n/" + line["code"].lower() + ".gif?'>"
    })
    """
        if score > 50:
        print("============通过===================")
        print(line["code"],line["codename"],score,zf)
        prettyTable.addRow([line["code"],line["codename"],score,zf])
    else:
        print("============未通过===================")
        print(line["code"], line["codename"], score, zf)
    """
    m += 1
#print(prettyTable.renderTable("score"))
redis.vset(ck, json.dumps(allrs))
